package com.zhy.sunmi;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;


import com.partner.tabtools.verticalTab.TabAdapter;
import com.partner.tabtools.verticalTab.TabView;
import com.partner.tabtools.verticalTab.VerticalTabLayout;
import com.zhy.sunmi.fragment.sell.SellFragment;
import com.zhy.sunmi.myView.MyViewPager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    List<String> tabsTitle = new ArrayList<>();
    List<Fragment> fragments = new ArrayList<>();
    MyViewPager vpAllTags;
    FragmentPagerAdapter fragmentPagerAdapter;
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getSupportActionBar()!=null){
            getSupportActionBar().hide();
        }
        hideBottomUIMenu();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        // 部分机型的statusbar会有半透明的黑色背景
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        // SDK21
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        //设置状态栏文字颜色
//        setAndroidNativeLightStatusBar(this,true);
        setAndroidNativeLightStatusBar(this,false);
        setContentView(R.layout.activity_main2);
        vpAllTags =findViewById(R.id.viewPager);
        findViewById(R.id.imageView2).setOnClickListener(v -> {
            startActivity(new Intent(this,MainActivity2.class));
        });
//        //获得控件
//        WebView webView = (WebView) findViewById(R.id.wv_webview);
//        //访问网页
//        webView.loadUrl("http://epos.spxus.uk/index.html");
//        //系统默认会通过手机浏览器打开网页，为了能够直接通过WebView显示网页，则必须设置
//        webView.setWebViewClient(new WebViewClient(){
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                //使用WebView加载显示url
//                view.loadUrl(url);
//                //返回true
//                return true;
//            }
//        });

//        findViewById(R.id.textView3).setOnClickListener(v -> {
//            findViewById(R.id.textView3).setSelected(true);
//
//        });

        VerticalTabLayout verticalTabLayout=findViewById(R.id.verticalTabLayout);
        tabsTitle = Arrays.asList("Sell" , "Reporting" , "Product", "Staff");
        for (String title : tabsTitle){
            Fragment fragment=new SellFragment();

//             = new TestFragment();
            Bundle bundle1 = new Bundle();
            bundle1.putString("_type", title);
            fragment.setArguments(bundle1);
            fragments.add(fragment);
        }


        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        };

        vpAllTags.setAdapter(fragmentPagerAdapter);
        verticalTabLayout.setupWithViewPager(vpAllTags);

       TabAdapter adapter = new TabAdapter() {
            @Override
            public int getCount() {
                return tabsTitle.size();
            }

            @Override
            public TabView.TabBadge getBadge(int position) {
                return null;
            }

            @Override
            public TabView.TabIcon getIcon(int position) {
                TabView.TabIcon.Builder builder=new TabView.TabIcon.Builder().setIconGravity(Gravity.TOP)
                        .setIconSize(20,20)
                        .setIconMargin(0);
                switch (position){
                    case 0:
                        builder.setIcon(R.mipmap.sell,R.mipmap.sell);
                        break;
                    case 1:
                        builder.setIcon(R.mipmap.reporting,R.mipmap.reporting);
                        break;
                    case 2:
                        builder.setIcon(R.mipmap.product,R.mipmap.product);
                        break;
                    case 3:
                        builder.setIcon(R.mipmap.staff,R.mipmap.staff);
                        break;
                }

                return builder.build();

//                return new TabView.TabIcon.Builder()
//                        .setIcon(R.mipmap.product,R.mipmap.product)
//                        .setIconGravity(Gravity.TOP)
//                        .setIconSize(13,10)
//                        .setIconMargin(0)
//                        .build();
            }

            @Override
            public TabView.TabTitle getTitle(int position) {

                return new TabView.TabTitle.Builder()
                        .setContent(tabsTitle.get(position))
                        .setTextSize(10)
                        .setTextColor(0xff6692b0,0xFF3A4955)
                        .build();
            }

            @Override
            public int getBackground(int position) {
//                if(position==2) return  R.color.white;
//                return R.color.white;
                return getResources().getColor(R.color.white2);
            }
        };

        verticalTabLayout.setTabAdapter(adapter);
    }

    public void setAndroidNativeLightStatusBar(boolean flag){
        setAndroidNativeLightStatusBar(this,flag);
    }


    private static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {

            Window _window = getWindow();
            WindowManager.LayoutParams params = _window.getAttributes();
            params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
            _window.setAttributes(params);
        }
    }
}

